﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;

namespace WSharp_Executor
{
    class Program
    {
        static int reletiveline = 0;
        static bool debugmode = true;
        static bool exeMain = false;
        static bool sntD = false;
        static Form progForm = new Form();
        static void Main(string[] args)
        {
            try
            {
                if (args[0] != "")
                {
                    string[] efile = File.ReadAllLines(args[0]);
                    foreach (string line in efile)
                    {
                        parseLine(line);
                    }
                }
                else
                {
                    Console.WriteLine("No File Sent in Arguments");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.Source);
                
            }
            Console.ReadKey();
        }
        static void parseLine(string input)
        {
            string cmdpeice = "";
            bool cmdcont = true;
            char crstart = char.Parse("{");
            string preparedInput = input.Replace("\t", "");
            foreach (char character in preparedInput)
            {
                string charin = character.ToString();
                if (charin == "{")
                {
                    cmdcont = false;
                }
                if (cmdcont == true)
                {
                    cmdpeice += character;
                }
                if (debugmode == true && exeMain == true && sntD == false)
                {
                    TextTools.advWrite("Text in Red are Debugging Lines.", ConsoleColor.Red);
                    TextTools.advWrite("Text in Gray are Program Lines.", ConsoleColor.Gray);
                    TextTools.advWrite("================================", ConsoleColor.Red);
                    sntD = true;
                }
            }
            reletiveline+=1;
            parseCommand(cmdpeice, reletiveline);
        }
        static void parseCommand(string command, int line)
        {
            if (debugmode == true && exeMain == true)
            {
                if (command != "}")
                {
                    TextTools.advWrite("Parsing: " + command + " @ " + line, ConsoleColor.Red);
                }
            }
            if (line == 1)
            {
                Console.Title = command;
            }
            else
            {
                string acommand = "";
                string aarg = "";
                bool acont = true;
                foreach (char character in command)
                {
                    string charin = character.ToString();
                    if (charin == "(")
                    {
                        acont = false;
                    }
                    if (charin == "=")
                    {
                        acont = false;
                    }
                    if (acont == false)
                    {
                        aarg += character;
                    }
                    if (acont == true)
                    {
                        acommand += character;
                    }
                }
                if (acommand != "}")
                {
                    if (debugmode == true && exeMain == true)
                    {
                        TextTools.advWrite("   Command: " + acommand, ConsoleColor.Red);
                        TextTools.advWrite("   Argument: " + aarg, ConsoleColor.Red);
                    }
                    parseAcommand(acommand, aarg);
                }
            }
        }
        static void parseAcommand(string cmd, string arg)
        {
            string preparedArg = arg.Replace(";", "");
            preparedArg = preparedArg.Replace("(", "");
            preparedArg = preparedArg.Replace(")", "");
            preparedArg = preparedArg.Replace("'", "");
            preparedArg = preparedArg.Replace("=", "");
            switch (cmd)
            {
                case "forms.run" :
                    Application.Run(progForm);
                    break;
                case "forms.addLabel" :
                    string[] spltArg = Regex.Split(preparedArg, ":");
                    Label tmpLabel = new Label();
                    tmpLabel.Text = spltArg[0];
                    tmpLabel.Width = int.Parse(spltArg[1]);
                    tmpLabel.Height = int.Parse(spltArg[2]);
                    tmpLabel.Location = new System.Drawing.Point(int.Parse(spltArg[3]), int.Parse(spltArg[4]));
                    tmpLabel.Font = new Font(spltArg[5], int.Parse(spltArg[6]), FontStyle.Regular);
                    progForm.Controls.Add(tmpLabel);
                    break;
                case "cons.writeLine" :
                    Console.WriteLine(preparedArg);
                    break;
                case "main" :
                    exeMain = true;
                    break;
                case "getTime" :
                    Console.WriteLine(DateTime.Now.TimeOfDay);
                    break;
                case "debugmode" :
                    if (preparedArg == "true")
                    {
                        debugmode = true;
                    }
                    else if (preparedArg == "false")
                    {
                        debugmode = false;
                    }
                    else
                    {
                        if (debugmode == true)
                        {
                            TextTools.advWrite("Error @ " + cmd, ConsoleColor.Red);
                        }
                    }
                    break;
                default :
                    TextTools.advWrite("Error @ " + cmd + ", Cant Parse Command", ConsoleColor.Gray);
                    break;
            }
        }
    }
    class TextTools
    {
        public static void advWrite(string input, ConsoleColor inputColour)
        {
            Console.ForegroundColor = inputColour;
            Console.WriteLine(input);
            Console.ResetColor();
        }
    }
}
